/**
 * Created by FreezeSoul on 2018/10/24.
 */
'use strict';
/**
 * Created by FreezeSoul on 2015/10/23.
 */
define([
    "app/core/base",
    "app/core/util",
    "../echartsMeta",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    "echarts"
], function (Chart, Util, Meta, $, _, JSON, log, ec) {

    var property = {
        name: 'eBarProperty',
        config: "property",
        option: []
            .concat($.extend(true, [], Meta.titleProperties))
            .concat($.extend(true, [], Meta.legendProperties))
            .concat($.extend(true, [], Meta.tooltipProperties))
            .concat($.extend(true, [], Meta.toolboxProperties))
            .concat([
                {
                    group: '绑定',
                    id: 'processBinding',
                    name: '进度绑定',
                    enable: true,
                    type: 'sortBinding',
                    dataset: '',
                    columns: [],
                    tooltip: '多进度值绑定，要求有三个绑定列，依次为标题、当前数、总数'
                }
            ]).concat($.extend(true, [], Meta.otherProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var bar = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._chart = null;
            this._container = null;
            this._option = {
                Binding: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    backgroundColor: '#0e2147',
                    xAxis: {
                        show: false
                    },
                    yAxis: [{
                        show: true,
                        data: ['太原市民政局', '太原市运输局', '太原市残联', '太原市报社', '太原市司法局'],
                        inverse: true,
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            show: false
                        },
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            color: '#fff',
                            textStyle: {
                                fontSize: 14,
                                color: '#fff',
                            }
                        },


                    }, {
                        show: true,
                        inverse: true,
                        data: [683, 234, 234, 523, 345],
                        axisLabel: {
                            textStyle: {
                                fontSize: 14,
                                color: '#fff',
                            },
                        },
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            show: false
                        },
                        axisTick: {
                            show: false
                        },

                    }],
                    series: [{
                        name: '条',
                        type: 'bar',
                        yAxisIndex: 0,
                        data: [70, 34, 60, 78, 69],
                        barWidth: 20,
                        itemStyle: {
                            normal: {
                                barBorderRadius: 30,
                                color: '#1089E7'
                            }
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'inside',
                                formatter: '{c}%',
                                textStyle: {
                                    fontSize: 14,
                                    color: '#fff',
                                }
                            }
                        },
                    }, {
                        name: '框',
                        type: 'bar',
                        yAxisIndex: 1,
                        barGap: '-100%',
                        data: [100, 100, 100, 100, 100],
                        barWidth: 30,
                        itemStyle: {
                            normal: {
                                color: 'none',
                                borderColor: '#1089E7',
                                borderWidth: 3,
                                barBorderRadius: 30,
                            }
                        }
                    }, ]
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
                this._chart = ec.init(element, null, {
                    renderer: 'canvas'
                });
                this._bindEvent();
            } catch (e) {
                log.error(e);
            }
        },
        //TODO:需要重写的部分
        _bindEvent: function () {
            var _this = this;
            this._chart.on("click", function (param) {
                if (_this._layout
                    && _this._layout.navigate
                    && _this._option.Events
                    && _this._option.Events.length > 0) {

                    param.event.event.preventDefault();
                }
            });
        },
        example: function () {
            try {
                this._chart.setOption(this._option.Option, true);
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        //TODO:需要重写的部分
        _bindData: function (data) {
            var _this = this;
            var bindProperty = _.findWhere(this._option.Extend.option, {id: 'processBinding'});
            if (!bindProperty || !bindProperty.enable) return;


            var dataSetCode = bindProperty.dataset;
            var columns = bindProperty.columns;
            if (columns.length !== 3) return;

            var table = data[dataSetCode];
            if (table) {
                _this._option.Option.yAxis[0].data = [];
                _this._option.Option.yAxis[1].data = [];
                _this._option.Option.series[0].data = [];

                $.each(table, function (j, row) {
                    _this._option.Option.yAxis[0].data.push(row[0]);
                    _this._option.Option.yAxis[1].data.push(row[2]);
                    var totalNum = row[2];
                    var currentNum = row[1];
                    var processNum = parseInt(currentNum/totalNum * 100);
                    _this._option.Option.series[0].data.push(processNum);
                });
            }
        },

        setData: function (data) {
            try {
                this._readProperty();

                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend) {

                    this._bindData(data);

                    this._readProperty();
                }

                var _this = this;
                setTimeout(function () {
                    _this._chart.setOption(_this._option.Option, true);
                });

            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
                this._chart.setTheme(theme);
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
                this._chart.resize();
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                this._chart.dispose();
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._container;
        },
        getChart: function () {
            return this._chart;
        }
    });

    return {
        Chart: bar,
        Property: property
    };
});
